{% extends 'base.html' %}

{% block title %}
    接口授权
{% endblock %}

{% block info %}
    接口授权
{% endblock %}

{% block info1 %}
    接口授权
{% endblock %}

{% block main %}
    <div class="row">
        <div class="col-lg-6">
            <div class="card m-b-30">
                <div class="card-body">
                    <h4 class="mt-0 header-title">授权列表</h4>
                    <p class="text-muted m-b-30 font-14">接口代码: <code>&#x3C;&#x73;&#x63;&#x72;&#x69;&#x70;&#x74;&#x20;&#x73;&#x72;&#x63;&#x3D;&#x22;//{{ request.headers.get('Host') }}{{ url_for('generate_js') }}&#x22;&#x3E;&#x3C;&#x2F;&#x73;&#x63;&#x72;&#x69;&#x70;&#x74;&#x3E;</code></p>
                    <table class="table">
                        <thead>
                        <tr>
                            <th>#</th>
                            <th>授权域名</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        {% for a in apis %}
                            <tr>
                                <td scope="row">{{ a.aid }}</td>
                                <td>{{ a.host }}</td>
                                <td>
                                    <button type="button" class="btn btn-sm btn-info" style="float: none; margin: 5px;"
                                            id="delapi" value="{{ url_for('del_api', aid=a.aid) }}"
                                            onclick="delapi(this.value)"><span class="ti-trash"></span></button>
                                </td>
                            </tr>
                        {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        <div class="col-6">
            <div class="card m-b-30">
                <div class="card-body">
                    <h4 class="mt-0 header-title">授权添加</h4>
                    <p class="text-muted m-b-30 font-14">添加<code>AHRID</code>平台的授权</p>
                    <div class="general-label">
                        <div class="form-inline">
                            <div class="form-group">
                                <label class="sr-only" for="username">授权域名/IP</label>
                                <input type="text" class="form-control ml-1" id="host" placeholder="Host/IP">
                            </div>
                            <div class="form-group">
                                <button type="submit" class="btn btn-sm btn-info ml-1" id="addapi">Add</button>
                            </div>
                    </div>
                </div>
            </div>
        </div> <!-- end col -->
    </div>
    </div>
{% endblock %}

{% block js %}
    <script>
        $("#addapi").click(function (event) {
            var url = "{{ url_for('add_api') }}";
            $.ajax({
                type: "POST",
                url: url,
                data: {
                    "host": $("#host").val(),
                },
                dataType: "json",
                success: function (json) {
                    message = JSON.parse(JSON.stringify(json))
                    if (message['success'] == 1) {
                        alert(message['message']);
                        window.location.href = message['url'];
                    } else {
                        alert(message['message']);
                    }
                }
            });
        });

        function delapi(url) {
            var isdel = confirm("是否确定删除该授权？");
            if (isdel) {
                $.ajax({
                    type: "POST",
                    url: url,
                    dataType: "json",
                    success: function (json) {
                        message = JSON.parse(JSON.stringify(json));
                        if (message['success'] == 1) {
                            alert(message['message']);
                            window.location.href = message['url'];
                        } else {
                            alert(message['message']);
                        }
                    }
                });
            }
        }
    </script>
{% endblock %}